﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default3 : System.Web.UI.Page
{
    SqlDataAdapter sda;
    SqlCommandBuilder scb;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            string strSQL = "SELECT [Yonghu].Xuehao 学号,Xingming 姓名,Duankouhao 端口号,[IP].IP IP地址 from [Yonghu],[IP] where [Yonghu].Xuehao=[IP].Xuehao order by [Yonghu].Xuehao asc" ;
            string strConn = @"Data Source=david\sql2005;Initial Catalog=IPSystem;Persist Security Info=True;User ID=sa;Password=123";
            sda = new SqlDataAdapter(strSQL, strConn);
            scb = new SqlCommandBuilder(sda);
            dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            //Session["ds "] = GridView1.DataSource;
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string xuehao = "";
        string xingming = "";
        string duankouhao = "";
        string ip = "";

        if (CheckBox1.Checked == true)
        { xuehao = "[dbo].[Yonghu].[Xuehao] 学号,"; }
        if (CheckBox2.Checked == true)
        { xingming = "Xingming 姓名,"; }
        if (CheckBox3.Checked == true)
        { duankouhao = "Duankouhao 端口号,"; }
        if (CheckBox4.Checked == true)
        { ip = "IP IP地址"; }                                   //判断结果列表中显示的列；

        string sort = "";
        switch(DDList2.SelectedIndex)
        {
            case 0: sort = "[Yonghu].Xuehao"; break;
            case 1: sort = "Xingming"; break;
            case 2: sort = "IP"; break;
        }                                                      //选择排序依据；

        string sort2 = "";
        switch (RadioButtonList1.SelectedIndex)
        {
            case 0: sort2 = "asc"; break;
            case 1: sort2 = "desc"; break;
        }

        //DataAccess.SQLTxt sql = new DataAccess.SQLTxt("System.Data.SqlClient","server=222.16.73.153;database=IPSystem;uid=sa;pwd=123");
        //sql.Exceute("SELECT '"+xuehao+"','"+xingming+"' ,'"+duankouhao+"','"+ip+"' FROM [IP],[Yonghu] where [dbo].[IP].Xuehao='"+TextBox2.Text+"'");
        //GridView2.DataSource = sql;
        string strSQL = "SELECT distinct " + xuehao + " " + xingming + "" + duankouhao + "" + ip + "  FROM [IP],[Yonghu] where [dbo].[Yonghu].Xuehao like '%" + TextBox2.Text.Trim() + "%' order by "+sort+" "+sort2+"";
        string strConn = @"Data Source=david\sql2005;Initial Catalog=IPSystem;Persist Security Info=True;User ID=sa;Password=123";

        sda = new SqlDataAdapter(strSQL, strConn);
        scb = new SqlCommandBuilder(sda);
        dt = new DataTable();
        sda.Fill(dt);
        //dt.PrimaryKey = new DataColumn[] { dt.Columns["Xuehao"] };
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox2.Text = "";

    }
}
/*
         <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Height="1px" Width="728px" AllowPaging="True" PageSize="100">
                <Columns>
                    <asp:TemplateField HeaderText="序号"><ItemTemplate> <%#Container.DataItemIndex+1 %></ItemTemplate></asp:TemplateField>
                    <asp:BoundField DataField="Xuehao" HeaderText="学号" SortExpression="Xuehao" />
                    <asp:BoundField DataField="Xingming" HeaderText="姓名" SortExpression="Xingming" />
                    <asp:BoundField DataField="Duankouhao" HeaderText="端口号" SortExpression="Duankouhao" />
                    <asp:BoundField DataField="IP" HeaderText="IP地址" SortExpression="IP" />
                </Columns>
                <FooterStyle HorizontalAlign="Center" />
            </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:IPSystemConnectionString %>"
                SelectCommand="SELECT [dbo].[Yonghu].[Xuehao]
      ,[IP]  
	,[Xingming]
	,[Duankouhao]
  FROM IP,Yonghu
where [dbo].[IP].Xuehao=[Yonghu].Xuehao">
            </asp:SqlDataSource>
 */